// 物流管理
const sqlOptions = require("../../sql");
const {format} = require("../../utils");
const logistics = require("express").Router()

logistics.post("/queryList", async (req, res) => {
    try {
        const data = req.body
        const arr = []
        for (const key in data) {
            if (data[key]) {
                arr.push(`${key} LIKE '%${data[key]}%'`)
            }
        }
        const str = arr.length ? `WHERE ${arr.join(" AND ")}` : ""
        const list = await req.db.query(`SELECT * FROM oms_order_logistics ${str}`)
        return res.status(200).send({
            code: 200,
            message: "",
            data: list
        })
    } catch (e) {
        // console.log(e)
        return res.status(500).send({
            code: 500,
            data: "",
            message: "系统错误"
        })
    }
})

// 物流更新
logistics.post("/update", async (req, res) => {
    try {
    //    oms_order_logistics
        const {logistics_result, id, logistics_status, order_id} = req.body
        const status = logistics_statusUpdate(logistics_status)
        await req.db.query(sqlOptions({
            name: 'oms_order_logistics',
            method: "update",
            key: {
                id
            },
            data: {
                logistics_result,
                logistics_status: logistics_statusUpdate(logistics_status),
                logistics_update_time: format()
            }
        }))
        if (status === "2") {
            await req.db.query(sqlOptions({
                name: "oms_order",
                method: "update",
                key: {
                    order_id
                },
                data: {
                    order_status: "2"
                }
            }))
        }
        console.log(status)
        return  res.status(200).send({
            code: 200,
            message: "更新成功",
            data: ""
        })

    } catch (e) {
        return res.status(500).send({
            code: 500,
            data: "",
            message: "系统错误"
        })
    }
})

// 没有物流只能写死模拟
function logistics_statusUpdate (logistics_status) {
    switch (logistics_status) {
        case "0":
            return "1"
        case "1":
            return "2"
        case "2":
            return "3"
        case "3":
            return "4"
        case "4":
            return "5"
        case "5":
            return "6"
        case "6":
            return "7"
        case "7":
            return "8"
        case "8":
            return "9"
        case "9":
            return "12"
    }
}

module.exports = logistics